# -*- coding: utf-8 -*-

from itsdangerous import TimedJSONWebSignatureSerializer, SignatureExpired, BadSignature, BadTimeSignature

from ..meta import Singleton


class DefaultSerializer(object, metaclass=Singleton):
    def __init__(self, key, expiration):
        self._key = key
        self._expiration = expiration

        self._serializer = TimedJSONWebSignatureSerializer(
            self._key,
            self._expiration,
        )

    def dump_user(self, user, type_='wechat'):
        return self._serializer.dumps({
            'uid': user.id,
            'user_type': type_,
            'site': user.site or None,
            'openid': user.openid,
            'unionid': user.unionid,
        }).decode('utf8')

    def loads(self, token):
        return self._serializer.loads(token)
